package com.EAsystem.service.Impl;

import com.EAsystem.entity.Major;
import com.EAsystem.mapper.MajorMapper;
import com.EAsystem.service.MajorService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class MajorServiceImpl implements MajorService {

    @Autowired
    private MajorMapper majorMapper;

    @Override
    public PageInfo<Major> findAllByPage(int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<Major> list = majorMapper.findAllWithPage();
        return new PageInfo<>(list);
    }

    @Override
    public PageInfo<Major> findByNamePage(Major major, int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<Major> list = majorMapper.findByNameWithPage(major);
        return new PageInfo<>(list);
    }

    @Override
    @Transactional
    public void save(Major major) {
        String now = new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new java.util.Date());
        major.setCreateTime(now);
        major.setUpdateTime(now);
        majorMapper.save(major);
    }

    @Override
    @Transactional
    public void delete(Integer id) {
        majorMapper.delete(id);
    }

    @Override
    @Transactional
    public void edit(Major major) {
        String now = new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new java.util.Date());
        major.setUpdateTime(now);
        majorMapper.edit(major);
    }

    @Override
    @Transactional
    public void batchDelete(java.util.List<Integer> ids) {
        majorMapper.batchDelete(ids);
    }

    @Override
    public List<Major> findAll() {
        return majorMapper.findAllWithPage();
    }

    @Override
    public Major findById(Integer id) {
        return majorMapper.findById(id);
    }
}